﻿using System;
using System.IO;
using System.Windows;


namespace ThePresenter.IO
{
	internal class SaveInformation : DependencyObject
	{
		#region Fields
		/// <summary>
		/// Identifies the <see cref="ThePresenter.IO.SaveInformation"/>.FileName dependency property.
		/// </summary>
		public static readonly DependencyProperty FileNameProperty = DependencyProperty.Register("FileName", typeof(string), typeof(SaveInformation));

		/// <summary>
		/// Identifies the <see cref="ThePresenter.IO.SaveInformation"/>.Writer dependency property.
		/// </summary>
		public static readonly DependencyProperty WriterProperty = DependencyProperty.Register("Writer", typeof(IWriter), typeof(SaveInformation));

		/// <summary>
		/// Identifies the <see cref="ThePresenter.IO.SaveInformation"/>.Config dependency property.
		/// </summary>
		public static readonly DependencyProperty ConfigProperty = DependencyProperty.Register("Config", typeof(DependencyObject), typeof(SaveInformation));
		#endregion


		#region Properties
		/// <summary>
		/// Gets or sets the name of the file. This is a dependency property.
		/// </summary>
		/// <value>The name of the file.</value>
		public string FileName
		{
			get { return (string)GetValue(FileNameProperty); }
			set { SetValue(FileNameProperty, value); }
		}

		/// <summary>
		/// Gets or sets the writer. This is a dependency property.
		/// </summary>
		/// <value>The writer.</value>
		public IWriter Writer
		{
			get { return (IWriter)GetValue(WriterProperty); }
			set { SetValue(WriterProperty, value); }
		}

		/// <summary>
		/// Gets or sets the config. This is a dependency property.
		/// </summary>
		/// <value>The config.</value>
		public DependencyObject Config
		{
			get { return (DependencyObject)GetValue(ConfigProperty); }
			set { SetValue(ConfigProperty, value); }
		}
		#endregion


		#region Methods
		/// <summary>
		/// Saves this instance.
		/// </summary>
		public void Save(Presentation presentation)
		{
			try
			{
				using (var fs = new FileStream(FileName, FileMode.Create))
				{
					Writer.Write(Config, presentation, fs);

					fs.Close();
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
			}
		}
		#endregion
	}
}
